package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login {
	public Login() {
		init();
	}

	JFrame frame;// 创建容器

	public void init() {
		// 各个组件
		frame = new JFrame("登录窗口");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		JButton b1 = new JButton("登录");
		JLabel l1 = new JLabel("账号:");
		JLabel l2 = new JLabel("密码:");
		JTextField t1 = new JTextField(12);
		JPasswordField t2 = new JPasswordField(12);
		t2.setEchoChar('*');
		// p1采用FlowLayout布局
		JPanel p1 = new JPanel(new GridLayout(2, 2));
		// 组件添加进p1
		p1.add(l1);
		p1.add(t1);
		p1.add(l2);
		p1.add(t2);
		frame.add(p1, BorderLayout.CENTER);
		frame.add(b1, BorderLayout.SOUTH);
		// b1注册事件监听器
		b1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				String name = t1.getText();
				String password = t2.getText();
				if (!(name.equals("123") && password.equals("456"))) {
					JOptionPane.showMessageDialog(null, "用户名密码错误！", "【出错啦】", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "用户名密码正确！", "【登录成功】", JOptionPane.PLAIN_MESSAGE);
					new Menu().setVisible(true);
					frame.setVisible(false);
				}
			}

		});
		frame.pack();
		frame.setVisible(true);
		double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frame.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 150));// 设定窗口出现位置

	}

}
